//
//  NSDetailNavView.m
//  Meboo
//
//  Created by kyo on 2024/3/18.
//

#import "NSDetailNavView.h"
@interface NSDetailNavView ()
//原因数组
@property (nonatomic, strong) NSMutableArray *reasonArr;
//返回
@property (nonatomic, strong) UIButton *backBtn;
//more
@property (nonatomic, strong) UIButton *moreBtn;
@end

@implementation NSDetailNavView

#pragma mark - Life Cycle
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

//如果想要判断设备是ipad，要用如下方法
- (BOOL)deviceIsIPad {
    NSString *deviceType = [UIDevice currentDevice].model;
    if([deviceType isEqualToString:@"iPad"]) {
        //iPad
        return YES;
    }
    return NO;
}

#pragma mark - Action
//右上角more点击 弹出底部系统选项
- (void)moreAction {
    __WeakSelf
    UIAlertController  *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    if ([self deviceIsIPad]){
        alert.popoverPresentationController.sourceView = self.currentViewController.view;
        alert.popoverPresentationController.sourceRect = CGRectMake(ScreenWidth/2,ScreenHeight/2,0,0);
        alert.popoverPresentationController.permittedArrowDirections = 0;
    }
    
    //举报
    UIAlertAction  *reportAction = [UIAlertAction actionWithTitle:@"Report" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf selectReasonAction];
    }];
    
    //拉黑
    UIAlertAction *BlockAction = [UIAlertAction actionWithTitle:@"Block" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[MBChatDataManager sharedManager] reportBlockUserWithUserId:self.userId reason:@"" type:1 success:^(BOOL success) {
            if (success) {
                [weakSelf deleteMessage:@"block success"];
            }
        }];
        /**
         埋点
         点击拉黑 Home_Profile_Block
         */
        [[NSIOApiManager sharedManager] setIOWithEventName:@"Home_Profile_Block"];
    }];
    
    UIAlertAction  *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    [alert addAction:reportAction];
    [alert  addAction:BlockAction];
    [alert addAction:cancelAction];
    [self.currentViewController presentViewController:alert animated:YES completion:nil];
    /**
     埋点
     点击更多 Home_Profile_More
     */
    [[NSIOApiManager sharedManager] setIOWithEventName:@"Home_Profile_More"];
}

//举报原因 弹出底部系统选项
- (void)selectReasonAction {
    __WeakSelf
    UIAlertController  *alert = [UIAlertController alertControllerWithTitle:@"What went wrong？" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    if ([self deviceIsIPad]){
        alert.popoverPresentationController.sourceView = self.currentViewController.view;
        alert.popoverPresentationController.sourceRect = CGRectMake(ScreenWidth/2,ScreenHeight/2,0,0);
        alert.popoverPresentationController.permittedArrowDirections = 0;
    }
    for (NSString *title in self.reasonArr) {
        UIAlertAction  *action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [[MBChatDataManager sharedManager] reportBlockUserWithUserId:weakSelf.userId reason:title type:4 success:^(BOOL success) {
                if (success) {
                    [weakSelf deleteMessage:@"report success"];
                    /**
                     埋点
                     举报成功 Home_Profile_Report
                     */
                    [[NSIOApiManager sharedManager] setIOWithEventName:@"Home_Profile_Reportsuccess"];
                }
            }];
        }];
        [alert addAction:action];
    }
    UIAlertAction  *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alert addAction:cancelAction];
    [self.currentViewController presentViewController:alert animated:YES completion:nil];
    /**
     埋点
     点击举报 Home_Profile_Report
     */
    [[NSIOApiManager sharedManager] setIOWithEventName:@"Home_Profile_Report"];
}

- (void)deleteMessage:(NSString *)noticeStr {
    __WeakSelf
    [[RCCoreClient sharedCoreClient] removeConversation:ConversationType_PRIVATE targetId:weakSelf.userId completion:^(BOOL ret) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.currentViewController.navigationController popViewControllerAnimated:YES];
            [MBProgressHUD showSuccessMessage:noticeStr];
            if (self.modelStyle == HomeModelDetail) {
                NSNotification *notify = [NSNotification notificationWithName:HOME_MODELDETAIL_LIKEPASS object:@"4"];
                [MBNotificationCenter postNotification:notify];//中心调方法，内容是参数
            } else if (self.modelStyle == WLMModelDetail) {
                NSNotification *notify = [NSNotification notificationWithName:WLM_MODELDETAIL_LIKEPASS object:@"2"];
                [MBNotificationCenter postNotification:notify];//中心调方法，内容是参数
            }
        });
    }];
}

- (void)backAction {
    [self.currentViewController.navigationController popViewControllerAnimated:YES];;
}

#pragma mark - UI
- (void)setupUI {
    self.backgroundColor = UIColor.clearColor;
    
    [self addSubview:self.backBtn];
    [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(MBWidthScale(20));
        make.bottom.mas_equalTo(MBHeightScale(-12));
        make.width.height.mas_equalTo(MBHeightScale(20));
    }];
    
    [self addSubview:self.moreBtn];
    [self.moreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(MBWidthScale(-20));
        make.centerY.equalTo(self.backBtn);
        make.width.height.mas_equalTo(MBHeightScale(24));
    }];
}

#pragma mark - Setter && Getter

- (UIButton *)moreBtn {
    if (!_moreBtn) {
        _moreBtn = [[UIButton alloc] init];
        [_moreBtn setImage:ImageWithName(@"home_detail_more") forState:0];
        [_moreBtn addTarget:self action:@selector(moreAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _moreBtn;
}

- (UIButton *)backBtn {
    if (!_backBtn) {
        _backBtn = [[UIButton alloc] init];
        [_backBtn setYXEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
        [_backBtn setImage:ImageWithName(@"mine_topic_back") forState:0];
        [_backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backBtn;
}

- (NSMutableArray *)reasonArr {
    if (!_reasonArr) {
        _reasonArr = [NSMutableArray arrayWithArray:@[@"Not my type",@"Fake profile! It’s a guy",@"Abusive behaviour or nudity",@"Scam",@"Sex services",@"Sale or mentions of drugs",@"Underage",]];
    }
    return _reasonArr;
}
@end
